package com.atguigu.daijia.driver.completableFutureTest;

import lombok.SneakyThrows;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @author 山顶洞人郑某
 * @className CompletableFutureTest.java
 * @date 2024 09 23
 */
public class CompletableFutureTest {

    @SneakyThrows
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {

            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("ssss");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "ok";
        }, executor);
       CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
           try {
               TimeUnit.SECONDS.sleep(1);
               System.out.println("aaa");
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
           return "ok2";
       },executor).thenCombine(future,(a,b)->{
           System.out.println(a);
           System.out.println(b);
           return "ok";
       });

    }
}
